package cn.com.zhuxulin.info.Utils;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public final class Http {
	private static HttpConnection conn=null;
	private static InputStream in=null;
	public static void doPost(String url){
	}
	public static String doGetString(String url,String encoding){
		try {
			conn=(HttpConnection)Connector.open(url);
			byte[] b=new byte[4096];
			ByteArrayOutputStream out=new ByteArrayOutputStream();
			in=Connector.openInputStream(url);
			//in=Connector.openDataInputStream(url);
			//System.out.println(conn.getResponseCode());
			while(in.read(b)!=-1){
				out.write(b);
			}
			String tmp=new String(out.toByteArray(), encoding);
			//System.out.println(tmp);
			return tmp;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static byte[] doGetByteArray(String url){
		try {
			conn=(HttpConnection)Connector.open(url);
			in=Connector.openInputStream(url);
			byte[] b=new byte[1024];
			ByteArrayOutputStream out=new ByteArrayOutputStream();
//			int len=0;
			while((in.read(b))!=-1){
				out.write(b);
			}
			return out.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static InputStream doGetInputStream(String url){
		try {
			return Connector.openInputStream(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
